<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>postmessage</title>
</head>
<body>
  <h1>postmessage A 页面</h1>
  <iframe
    src="http://localhost:4000/b.html"
    id="frame"
    frameborder="10"
    onload="load()"
  ></iframe>
  <script>
    function load() {
      let frame = document.getElementById("frame");
      var iwindow = frame.contentWindow;

      iwindow.postMessage({
        type: 'a',
        data: "我是 a 发的消息",
      }, "http://localhost:4000")
    }

    window.addEventListener('message', function(event){
      const { type, data } =  event.data;
      if (!event.data || type !== 'b') {
        return;
      }
      console.log(data);
    })
  </script>
</body>
</html>